<template>
  <div class="pageModel">
    <div class="topBtn">
      <div style="display: flex">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="background: #2f54eb"
          @click="() => handleOperate()"
           size="small"
          >新增列表页面</el-button
        >
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="() => handleDelete()"
          :disabled="!multipleSelection.length"
           size="small"
          >删除</el-button
        >
      </div>
      <el-input
        placeholder="请输入关键字"
        v-model="queryParams.name"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          type="primary"
          style="background: #2f54eb"
          @click="getList"
        ></el-button>
      </el-input>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
        size="mini"
        v-loading="loading"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column prop="date" label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              (scope.$index + 1)
            }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="页面名称" align="left">
        </el-table-column>
        <el-table-column prop="code" label="页面编码" align="left">
        </el-table-column>
        <el-table-column
          prop="status"
          label="页面类型"
          align="center"
          :formatter="
            (row) => selectDictLabel(dict.type.pt_app_page_type, row.type)
          "
        >
        </el-table-column>
        <el-table-column
          prop="status"
          label="页面状态"
          align="center"
          :formatter="
            (row) => selectDictLabel(dict.type.pt_app_enable_status, row.status)
          "
        >
        </el-table-column>
        <el-table-column
          prop="detectionStatus"
          label="检出状态"
          align="center"
          :formatter="
            (row) =>
              selectDictLabel(
                dict.type.pt_app_form_detection_status,
                row.detectionStatus
              )
          "
        >
        </el-table-column>
        <el-table-column prop="detectedBy" label="检出人" align="center">
        </el-table-column>
        <!-- <el-table-column prop="sort" label="排序" align="center" width="120">
        </el-table-column> -->
        <el-table-column prop="operate" label="操作" align="center">
          <template slot-scope="scope"
            ><div>
              <el-button type="text" @click="() => handleCheck(scope.row)">{{
                scope.row.detectionStatus === "0" ? "检出" : "检入"
              }}</el-button>
              <el-button type="text" @click="handleOperate(scope.row)"
                >编辑</el-button
              >
              <el-dropdown
                placement="bottom"
                @command="(value) => generateEr(scope.row, value)"
              >
                <el-button type="text"
                  >更多<i class="el-icon-arrow-down"></i
                ></el-button>
                <el-dropdown-menu
                  slot="dropdown"
                  style="min-width: 100px; text-align: center"
                >
                  <el-dropdown-item command="1">测试</el-dropdown-item>
                  <el-dropdown-item command="2">复制</el-dropdown-item>
                  <el-dropdown-item command="3">{{
                    scope.row.status === "1" ? "停用" : "启用"
                  }}</el-dropdown-item>
                  <el-dropdown-item command="4">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <addOrEditModal
      :open="open"
      :close="close"
      :getList="getList"
      :row="row"
      v-if="open"
      :appId="appId"
      :modelType="modelType"
    />
  </div>
</template>

<script>
import {
  appPageModelList,
  appPageModelDelete,
  checkInAndOut,
  openInClose,
} from "@/api/app/pageModel";
import addOrEditModal from "./addOrEditModal.vue";
export default {
  name: "pageModel",
  components: {
    addOrEditModal,
  },
  dicts: [
    "pt_app_page_type",
    "pt_app_form_detection_status",
    "pt_app_enable_status",
  ],

  data() {
    return {
      loading: false,
      multipleSelection: [],
      total: 0,
      queryParams: { name: undefined, pageNum: 1, pageSize: 10 },
      row: undefined,
      tableData: [],
      open: false,
      modelType:undefined
    };
  },
  props: {
    appId: {
      type: String,
      default: "",
    },
  },
  mounted() {
    this.getList();
  },

  methods: {
    async getList() {
      this.loading = true;
      const res = await appPageModelList({
        ...this.queryParams,
        appId: this.appId,
      });
      if (res.code == 200) {
        this.tableData = res.rows;
        this.total = res.total;
      }
      this.loading = false;
    },
    close() {
      this.open = false;
      this.getList();
    },
    handleOperate(row) {
      this.row = row;
      this.open = true;
      this.modelType = undefined
        sessionStorage.removeItem("pageModel");
    },
    handleDelete(row) {
      this.$confirm(
        `此操作将永久删除${row ? "该条" : "选中"}数据, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          const ids = [];
          if (!row) {
            this.multipleSelection?.forEach((item) => ids.push(item.id));
          }
          const res = await appPageModelDelete(!row ? ids.toString() : row.id);
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.getList();
          }
        })
        .catch(() => {});
    },
    async handleCheck(row) {
      const res = await checkInAndOut(row.id, row.detectionStatus);
      if (res.code === 200) {
        this.$message.success(
          row.detectionStatus === "0" ? "检出成功" : "检入成功"
        );
        this.getList();
      }
    },
    async generateEr(row, value) {
      if (value === "1") {
        this.$message.warning("敬请期待");
      }
      if (value === "2") {
        this.row = row;
        this.open = true;
        this.modelType = 'copy'
          sessionStorage.removeItem("pageModel");
      }
      if (value === "3") {
        const res = await openInClose(row.id, row.status);
        if (res.code === 200) {
          this.$message.success(row.status === "1" ? "停用成功" : "启用成功");
          this.getList();
        }
      }
      if (value === "4") {
        this.handleDelete(row);
      }
    },
    handleSelectionChange(arr) {
      this.multipleSelection = arr;
    },
  },
};
</script>

<style lang="scss" scoped>
.pageModel {
  .topBtn {
    display: flex;
    justify-content: space-between;
    align-items: center;
    ::v-deep .input-with-select {
      width: 300px;
    }
    ::v-deep .el-icon-search {
      color: #fff;
    }
  }
  .pagination {
    height: 50px;
  }
  .table {
    margin-top: 8px;
  }
}
</style>
